
package com.shai.wool.channel.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.shai.wool.channel.entity.ChannelInfoVO;
import com.shai.wool.channel.param.ChannelInfoParam;
import com.shai.wool.channel.service.ChannelInfoService;
import com.shai.wool.common.controller.BaseController;
import com.shai.wool.common.param.SessionUserInfo;
import com.shai.wool.sensitiveword.SensitiveWordUtils;

/**
 * 渠道信息Controller
 *
 * @author 朱国军
 * @since 1.0
 * @version 2015年12月27日 朱国军
 */
@Controller
public class ChannelInfoController extends BaseController {

    private Logger logger = LoggerFactory.getLogger(ChannelInfoController.class);

    @Autowired
    private ChannelInfoService channelInfoService;

    private final static String LIST_PAGE = "/channel/channel_list_ajax";

    private final static String VIEW_PAGE = "/channel/channel_view";

    private final static String UPDATE_PAGE = "/channel/channel_edit";

    private static final String MY_LIST_PAGE = "/account/my_channel";
    
    @RequestMapping(value = "/check/channel/toInsert")
    public ModelAndView intoInsert() {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        ChannelInfoVO channelInfoVO = new ChannelInfoVO();
        resultMap.put("channel", channelInfoVO);
        return new ModelAndView(UPDATE_PAGE, resultMap);
    }
    
    @RequestMapping(value = "/check/channel/toUpdate/{id}")
    public ModelAndView intoUpdate(@PathVariable String id) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        ChannelInfoVO channelInfoVO = channelInfoService.findById(id);
        resultMap.put("channel", channelInfoVO);
        return new ModelAndView(UPDATE_PAGE, resultMap);
    }
    
    @RequestMapping(value = "/check/channel/update", produces = "application/json;charset=utf-8;")
    @ResponseBody
    public String update(ChannelInfoVO channelInfoVO) {
        SessionUserInfo seUser = (SessionUserInfo) session.getAttribute("sessionUserInfo");
        Map<String, Object> resultMap = new HashMap<String, Object>();
        
        if (SensitiveWordUtils.isContaintSensitiveWord(channelInfoVO.getChannelDetail(), 1)) {
            resultMap.put("result", false);
            resultMap.put("message", "详细介绍中包含敏感词汇" + SensitiveWordUtils.getSensitiveWord(channelInfoVO.getChannelDetail(), 1) + "，请检查后再保存");
            return JSONObject.toJSONString(resultMap);
        }
        
        if (SensitiveWordUtils.isContaintSensitiveWord(channelInfoVO.getChannelName(), 1)) {
            resultMap.put("result", false);
            resultMap.put("message", "商户名信息中包含敏感词汇" + SensitiveWordUtils.getSensitiveWord(channelInfoVO.getChannelName(), 1) + "，请检查后再保存");
            return JSONObject.toJSONString(resultMap);
        }

        if (SensitiveWordUtils.isContaintSensitiveWord(channelInfoVO.getContactName(), 1)) {
            resultMap.put("result", false);
            resultMap.put("message", "联系人信息中包含敏感词汇" + SensitiveWordUtils.getSensitiveWord(channelInfoVO.getContactName(), 1) + "，请检查后再保存");
            return JSONObject.toJSONString(resultMap);
        }

        if (SensitiveWordUtils.isContaintSensitiveWord(channelInfoVO.getContactPhone(), 1)) {
            resultMap.put("result", false);
            resultMap.put("message", "联系电话中包含敏感词汇" + SensitiveWordUtils.getSensitiveWord(channelInfoVO.getContactPhone(), 1) + "，请检查后再保存");
            return JSONObject.toJSONString(resultMap);
        }
        
        try {
            Integer affectCount = 0;
            if (channelInfoVO != null && StringUtils.isNotBlank(channelInfoVO.getChannelId())) {
                affectCount = channelInfoService.update(channelInfoVO);
            } else if (channelInfoVO != null) {
                channelInfoVO.setChannelId(UUID.randomUUID().toString());
                channelInfoVO.setPublishUser(seUser.getUserId());
                channelInfoVO.setDataStatus(0);
                affectCount = channelInfoService.insert(channelInfoVO);
            }
            if (affectCount != null && affectCount == 1) {
                resultMap.put("result", true);
                resultMap.put("message", "新增或修改渠道信息成功");
            } else {
                resultMap.put("result", false);
                resultMap.put("message", "新增或修改渠道信息失败");
            }
        } catch (Exception e) {
            logger.error("新增或修改渠道信息出错", e);
            resultMap.put("result", false);
            resultMap.put("message", "新增或修改渠道信息出错：" + e.getLocalizedMessage());
        }
        return JSONObject.toJSONString(resultMap);
    }
    
    @RequestMapping(value = "/admin/channel/delete/{id}", produces = "application/json;charset=utf-8;")
    @ResponseBody
    public String delete(@PathVariable String id) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            Integer deleteCount = channelInfoService.delete(id);
            if (deleteCount != null && deleteCount == 1) {
                resultMap.put("result", true);
                resultMap.put("message", "删除渠道信息成功");
            } else {
                resultMap.put("result", false);
                resultMap.put("message", "删除渠道信息失败");
            }
        } catch (Exception e) {
            logger.error("删除渠道信息出错", e);
            resultMap.put("result", false);
            resultMap.put("message", "删除渠道信息出错：" + e.getLocalizedMessage());
        }
        return JSONObject.toJSONString(resultMap);
    }

    @RequestMapping(value = "/uncheck/channel/list")
    public ModelAndView queryList(ChannelInfoParam channelInfoParam) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        List<Map<String, Object>> dataList = channelInfoService.queryMapByPage(channelInfoParam);
        resultMap.put("dataList", dataList);
        resultMap.put("paramMap", channelInfoParam);
        return new ModelAndView(LIST_PAGE, resultMap);
    }

    @RequestMapping(value = "/account/channel/list")
    public ModelAndView queryMyList(ChannelInfoParam channelInfoParam) {
        SessionUserInfo seUser = (SessionUserInfo) session.getAttribute("sessionUserInfo");
        channelInfoParam.setPublishUser(seUser.getUserId());
        
        Map<String, Object> resultMap = new HashMap<String, Object>();
        List<Map<String, Object>> dataList = channelInfoService.queryMapByPage(channelInfoParam);
        resultMap.put("dataList", dataList);
        resultMap.put("paramMap", channelInfoParam);
        return new ModelAndView(MY_LIST_PAGE, resultMap);
    }
    
    @RequestMapping(value = "/uncheck/channel/view/{id}")
    public ModelAndView viewConfigInfo(@PathVariable String id) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        ChannelInfoVO channelInfoVO = channelInfoService.findById(id);
        resultMap.put("channel", channelInfoVO);
        resultMap.put("discussType", 3);
        resultMap.put("discussTargetId", channelInfoVO.getChannelId());
        return new ModelAndView(VIEW_PAGE, resultMap);
    }
}
